//********************************************************************
// RSS Radio - Desktop and mobile device podcast client
// Copyright (C) 2005, 2006 Wes Henderson <whenders@gmail.com> and
// Contributors
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more episode.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
//********************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RSSRadio
{
	public partial class MainForm : Form
	{
		private MenuItem[] PodcastMenuItems;
        MenuItem[] SubscriptionActionMenus;

		public MainForm()
		{
			InitializeComponent();

			// Note: update this list if adding/removing podcast command menu items
			PodcastMenuItems = new MenuItem[2] { MnuDeletePodcast, MnuPodcastDetails };
            SubscriptionActionMenus = new MenuItem[] { menuDeleteFeed, menuDownloadLatest };

			if (PodcastView.Items.Count == 0)
				GuiUtil.DisableMenuItems(PodcastMenuItems);

            subscriptionView.LoadSubscriptions();
            setVisibleMenus();
            subscriptionView.SelectedItemChanged += new EventHandler<EventArgs>(subscriptionView_SelectedItemChanged);
            menuDownloadLatest.Enabled = subscriptionView.CanDownloadPodcast;


        }


        private void subscriptionView_SelectedItemChanged(object Source, EventArgs e)
        {
            menuDownloadLatest.Enabled = subscriptionView.CanDownloadPodcast;
        }

        private void MnuExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}
        /*
        private void showSubscriptions(bool autoload)
        {
            SubscriptionForm subscriptionForm = new SubscriptionForm(autoload);
            subscriptionForm.Closing += new CancelEventHandler(PodcastView.ReloadItemsAndRepaint);
            subscriptionForm.ShowDialog();
        }
        */
        private void tabChanged(object sender, EventArgs e)
        {
            setVisibleMenus();
        }


        #region Podcast menus
        private void MnuDelete_Click(object sender, EventArgs e)
        {
            PodcastView.DeleteSelectedPodcast();
        }

        /// <summary>
        /// Refresh the list of displayed podcasts at user's request from the main menu
        /// </summary>
        private void MnuRefreshPodcasts_Click(object sender, EventArgs e)
        {
            Globals.Load();

            if (PodcastView.Items.Count == 0)
                GuiUtil.DisableMenuItems(PodcastMenuItems);
            else
                GuiUtil.EnableMenuItems(PodcastMenuItems);

            PodcastView.ReloadItemsAndRepaint();
        }

        private void MnuDetails_Click(object sender, EventArgs e)
        {
            PodcastView.ShowSelectedPodcastDetails();
        }

        #endregion

        #region Feed Menus
        private void menuAddFeed_Click(object sender, EventArgs e)
        {
            if (Globals.getSubscriptionManager().CheckIfAllLoaded())
            {
                FeedUrlInputDlg feedDlg = new FeedUrlInputDlg(subscriptionView);
                feedDlg.ShowDialog();
                EnableFeedMenus();
                subscriptionView.Refresh();
            }
            else
                MessageBox.Show(SubscriptionListView.UserWaitUntilLoadedMsg);
        }
        private void menuEditFeed_Click(object sender, EventArgs e)
        {

            if (subscriptionView.Items.Count == 0)
                return;

            subscriptionView.EditSelectedItem();
            EnableFeedMenus();

        }
        private void menuDeleteFeed_Click(object sender, EventArgs e)
        {
            subscriptionView.DeleteSelectedItem();
            EnableFeedMenus();
        }

        private void menuDownloadLatest_Click(object sender, EventArgs e)
        {
            subscriptionView.DownloadSelectedLatestEpisode();
        }

        private void menuRefreshFeeds_Click(object sender, EventArgs e)
        {
            subscriptionView.LoadSubscriptions();
            Globals.getSubscriptionManager().refreshFeeds();
            EnableFeedMenus();
            subscriptionView.Refresh();
        }
        #endregion

        #region Utility methods

        private void setVisibleMenus()
        {
            this.menu.MenuItems.Clear();
            //this.menu.MenuItems.Add(this.menusplitter1);
            if (tabNavigation.SelectedIndex == 0)
            {
                this.menu.MenuItems.Add(this.MnuDeletePodcast);
                this.menu.MenuItems.Add(this.MnuPodcastDetails);
                this.menu.MenuItems.Add(this.MnuRefreshPodcasts);
            }
            else if (tabNavigation.SelectedIndex == 1)
            {
                this.menu.MenuItems.Add(this.menuAddFeed);
                this.menu.MenuItems.Add(this.menuEditFeed);
                this.menu.MenuItems.Add(this.menuDeleteFeed);
                this.menu.MenuItems.Add(this.menuDownloadLatest);
                this.menu.MenuItems.Add(this.menuRefreshFeeds);
            }
            this.menu.MenuItems.Add(this.menuSplitter2);
            this.menu.MenuItems.Add(this.menuDownloads);
            this.menu.MenuItems.Add(this.menuSettings);
            this.menu.MenuItems.Add(this.MnuExit);
        }        
        public void EnableFeedMenus()
        {
            if (subscriptionView.Items.Count == 0)
                GuiUtil.DisableMenuItems(SubscriptionActionMenus);
            else
                GuiUtil.EnableMenuItems(SubscriptionActionMenus);
        }
        #endregion

        private void menuDownloads_Click(object sender, EventArgs e)
        {
            if (Globals.ActivePodcastDownloaders.Count > 0)
            {
                DownloadManager manager = new DownloadManager();
                manager.ShowDialog();
            }
            else
                MessageBox.Show("No active downloads.");
        }

        private void menuGetUpdates_Click(object sender, EventArgs e)
        {
            Globals.getSubscriptionManager().DownloadLatestEpisodes();
        }

        private void tabNavigation_KeyDown(object sender, KeyEventArgs e)
        {
            if (tabNavigation.SelectedIndex == 0)
            {
                PodcastView.ProxyKeyDown( e);
            }
            else if (tabNavigation.SelectedIndex == 1)
            {
                subscriptionView.ProxyKeyDown(e);
            }
        }

        private void subscriptionView_ListChanged(object sender, EventArgs e)
        {
            EnableFeedMenus();
        }

        private void menuSettings_Click(object sender, EventArgs e)
        {
            Settings settings = new Settings();
            settings.ShowDialog();

        }



    }
}